package demo1;

import java.util.Arrays;
import java.util.Comparator;

class NameCmp implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name,int age,int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
        //return this.age - o.age;
    }
}
public class Test {

    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length - 1;i++) {
            for (int j = 0; j < array.length - i - 1;j++) {
                if (array[j].compareTo(array[j+1]) > 0) {
                    Comparable ret = array[j];
                    array[j] = array[j+1];
                    array[j+1] = ret;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new
                Student("san",19,10);
        students[1] = new
                Student("lisi",58,20);
        students[2] = new
                Student("abc",39,5);
        NameCmp nameCmp = new NameCmp();
        Arrays.sort(students,nameCmp);
        System.out.println(Arrays.toString(students));

        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.age - o2.age;
            }
        });
        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student[] students = new Student[3];
        students[0] = new
                Student("san",19,10);
        students[1] = new
                Student("lisi",58,20);
        students[2] = new
                Student("abc",39,5);
        sort(students);
        System.out.println(Arrays.toString(students));
    }

}
